package com.yeskery.nut.extend.s3;

import com.yeskery.nut.plugin.ApplicationContextSupportBasePlugin;
import com.yeskery.nut.plugin.PluginBeanMetadata;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Asw S3 插件
 * @author sprout
 * @version 1.0
 * 2025-05-14 14:26
 */
public class AwsS3Plugin extends ApplicationContextSupportBasePlugin {

    /** 访问key */
    private final String accessKey;

    /** 访问密钥 */
    private final String secretKey;

    /** 区域 */
    private final String region;

    /** 端点 */
    private final String endpoint;

    /** 服务配置 */
    private S3Configuration serviceConfiguration;

    /**
     * 构建Asw S3 插件
     * @param accessKey 访问key
     * @param secretKey 访问密钥
     * @param endpoint  端点
     * @param region    区域
     * @param serviceConfiguration 服务配置
     */
    public AwsS3Plugin(String accessKey, String secretKey, String endpoint, String region, S3Configuration serviceConfiguration) {
        this(accessKey, secretKey, endpoint, region);
        this.serviceConfiguration = serviceConfiguration;
    }

    /**
     * 构建Asw S3 插件
     * @param accessKey 访问key
     * @param secretKey 访问密钥
     * @param endpoint  端点
     * @param region    区域
     */
    public AwsS3Plugin(String accessKey, String secretKey, String endpoint, String region) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.endpoint = endpoint;
        this.region = region;
    }

    @Override
    protected Collection<PluginBeanMetadata> getRegisterPluginBeanMetadata() {
        if (serviceConfiguration == null) {
            serviceConfiguration = S3Configuration.builder().build();
        }
        List<PluginBeanMetadata> pluginBeanMetadataList = new ArrayList<>(2);
        pluginBeanMetadataList.add(new PluginBeanMetadata("s3Client", buildS3Client(serviceConfiguration), S3Client.class));
        pluginBeanMetadataList.add(new PluginBeanMetadata("s3PreSigner", buildS3Presigner(serviceConfiguration), S3Presigner.class));
        return pluginBeanMetadataList;
    }

    /**
     * 构建S3客户端
     * @param s3Configuration S3配置
     * @return S3客户端
     */
    private S3Client buildS3Client(S3Configuration s3Configuration) {
        AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey);
        return S3Client.builder()
                .endpointOverride(URI.create(endpoint))
                .credentialsProvider(StaticCredentialsProvider.create(credentials))
                .region(Region.of(region))
                .serviceConfiguration(s3Configuration)
                .build();
    }

    /**
     * 构建S3 PreSigner
     * @param s3Configuration S3配置
     * @return S3 PreSigner
     */
    private S3Presigner buildS3Presigner(S3Configuration s3Configuration) {
        AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey);
        return S3Presigner.builder()
                .endpointOverride(URI.create(endpoint))
                .credentialsProvider(StaticCredentialsProvider.create(credentials))
                .region(Region.of(region))
                .serviceConfiguration(s3Configuration)
                .build();
    }
}
